package imanager.showcases.agenda;

import imanager.showcases.agenda.client.AgendaApplication;
import imanager.showcases.agenda.client.ListaComprasDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.googlecode.imanager.InterfaceManager;
import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.Document;
import com.googlecode.imanager.client.action.Action;
import com.googlecode.imanager.client.event.EvaluationPhase;
import com.googlecode.imanager.client.event.PhaseEvent;
import com.googlecode.imanager.client.exception.IMException;
import com.googlecode.imanager.client.io.Path;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class AgendaSwingApplication extends AgendaApplication {

    private static final long serialVersionUID = 1L;
    
    public AgendaSwingApplication() {
        configure();
    }

    public static void main(String[] args) throws IMException {
        InterfaceManager manager = InterfaceManager.getInstance();
        manager.generateSwing(new AgendaSwingApplication());
    }
    
    @Override
    protected void onPhaseEvent(PhaseEvent e) {
        if (e.getTarget() == getListaComprasDocument()
                && e.getEventType() == EvaluationPhase.WINDOW_HIDE){
            // if the main window hides, program should exit
            System.exit(0);
        }
        super.onPhaseEvent(e);
    }

    private void configure() {
        String basePath = getClass().getResource("./").toExternalForm();
        setBasePath(new Path(basePath));
        
        // displays the main window
        getListaComprasDocument().setVisible(true);
    
        setExportarPdfAction(new Action() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void doExecute(Control sender) throws Exception {
                exibirRelatorio(new ListaComprasDocument(), "pdf");
            }
        });
        
        setExportarXlsAction(new Action() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void doExecute(Control sender) throws Exception {
                exibirRelatorio(new ListaComprasDocument(), "xls");
            }
        });        
    }
    

    private static void exibirRelatorio(Document document, String extensao) throws IOException, IMException {
        File file = new File("../../hello." + extensao);
        
        OutputStream output = new FileOutputStream(file);
        try {
            InterfaceManager manager = InterfaceManager.getInstance();
        
            if (extensao.equals("pdf")){
                manager.generatePdf(document, output);
            }
            else if (extensao.equals("xls")){
                manager.generateXls(document, output);
            }

        } finally {
            output.close();
        }
        
        Runtime.getRuntime().exec(new String[]{
                "cmd", "/C", "start", "explorer", file.getAbsolutePath()
        });
    }

}
